﻿Imports System.Data.OleDb
Public Class Edit_Profile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'The following code makes sure that the details of the user are populated when the page loads.

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TravellingAloneConnectionString").ConnectionString)
        Dim SqlString As String = "Select * From profile Where traveller_name = @f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        oleDbConn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader()

        'Checking if dr can bring back any records

        If dr.HasRows Then

            dr.Read()
            If Not IsDBNull(dr("first_name")) Then tb_first_name.Text = dr("first_name")
            If Not IsDBNull(dr("last_name")) Then tb_last_name.Text = dr("last_name")
            If Not IsDBNull(dr("nationality")) Then tb_nationality.Text = dr("nationality")
            If Not IsDBNull(dr("first_name")) Then tb_short_description.Text = dr("short_description")

        End If



    End Sub

    Protected Sub btn_save_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_save.Click

        'This is the code that runs every time the user clicks the "Save Changes" button in their profile.
        'All the details are updated and the record in the database is changed.

        Dim myGuid As New System.Guid
        myGuid = Guid.NewGuid()
        Dim newFileName As String
        newFileName = myGuid.ToString() & ".jpg"

        If f_photo.HasFile() = True Then

            f_photo.SaveAs(Request.MapPath("Pictures") & "/" & newFileName)

        End If


        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TravellingAloneConnectionString").ConnectionString)
        Dim SqlString As String = "Update profile Set first_name = @f1, last_name = @f2, nationality = @f3, short_description = @f4, picture = @f5 Where traveller_name = @f6"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_first_name.Text)
        cmd.Parameters.AddWithValue("@f2", tb_last_name.Text)
        cmd.Parameters.AddWithValue("@f3", tb_nationality.Text)
        cmd.Parameters.AddWithValue("@f4", tb_short_description.Text)
        cmd.Parameters.AddWithValue("@f5", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f6", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("Profile.aspx?TravellerName=" & User.Identity.Name)

    End Sub

End Class